package com.movitech.mobile.cz.base.purchase.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.google.common.collect.Maps;
import com.movitech.mobile.cz.base.common.util.CommonConstants;
import com.movitech.mobile.cz.base.common.util.DateUtils;
import com.movitech.mobile.cz.base.common.util.StringUtils;
import com.movitech.mobile.cz.base.purchase.bizvo.CalibrationInfoReqVO;
import com.movitech.mobile.cz.base.purchase.entity.WFlowFormBackBid;
import com.movitech.mobile.cz.base.purchase.entity.ext.ExtWFlowFormBackBid;
import com.movitech.mobile.cz.base.purchase.entity.resultType.IdName;
import com.movitech.mobile.cz.base.purchase.mapper.WFlowFormBackBidMapper;
import com.movitech.mobile.cz.base.purchase.mapper.ext.ExtWFlowFormBackBidMapper;
import com.movitech.mobile.cz.base.purchase.service.WFlowFormBackBidService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 流程表单-供应商入围-回标 服务实现类
 * </p>
 *
 * @author Movitech Mobile
 * @since 2018-03-19
 */
@Service
public class WFlowFormBackBidServiceImpl extends ServiceImpl<WFlowFormBackBidMapper, WFlowFormBackBid> implements WFlowFormBackBidService {
    @Autowired
    private ExtWFlowFormBackBidMapper extWFlowFormBackBidMapper;
    @Override
    public List<WFlowFormBackBid> getAwardBidForm(String formId) {
        WFlowFormBackBid wFlowFormBackBid = new WFlowFormBackBid();
        wFlowFormBackBid.setAwardBidOrNot(CommonConstants.BID_OK);
        wFlowFormBackBid.setFlowFormId(formId);
        wFlowFormBackBid.setDelFlag(CommonConstants.DEL_FLAG_NORMAL);
        EntityWrapper entityWrapper = new EntityWrapper();
        entityWrapper.setEntity(wFlowFormBackBid);
        return selectList(entityWrapper);
    }

    /**
     * 删除定标信息数据
     * @param calibrationInfoReqVO
     */
    @Override
    public void deleteByformIdOrSupplierId(CalibrationInfoReqVO calibrationInfoReqVO) {

        if(StringUtils.isNotEmpty(calibrationInfoReqVO.getId())){
            WFlowFormBackBid wFlowFormBackBid = new WFlowFormBackBid();
            wFlowFormBackBid.setId(calibrationInfoReqVO.getId());
            wFlowFormBackBid.setDelFlag(CommonConstants.DONG_FLAG);
            updateById(wFlowFormBackBid);
        }else{
            WFlowFormBackBid wFlowFormBackBid = new WFlowFormBackBid();
            wFlowFormBackBid.setDelFlag(CommonConstants.DONG_FLAG);
            EntityWrapper entityWrapper = new EntityWrapper();
            WFlowFormBackBid where = new WFlowFormBackBid();
            where.setFlowFormId(calibrationInfoReqVO.getFlowFormId());
            where.setSupplierId(calibrationInfoReqVO.getSupplierId());
            entityWrapper.setEntity(where);
            update(wFlowFormBackBid,entityWrapper);
        }
    }

    @Override
    public List<WFlowFormBackBid> getByFormId(String formId) {
        WFlowFormBackBid wFlowFormBackBid = new WFlowFormBackBid();
        wFlowFormBackBid.setFlowFormId(formId);
        wFlowFormBackBid.setDelFlag(CommonConstants.DEL_FLAG_NORMAL);
        EntityWrapper entityWrapper = new EntityWrapper();
        entityWrapper.setEntity(wFlowFormBackBid);
        return selectList(entityWrapper);
    }


    @Override
    public List<ExtWFlowFormBackBid> getExtBackBidByMap(Map<String, Object> map) {
        return extWFlowFormBackBidMapper.getBackBidByMap(map);
    }


    @Override
    public List<ExtWFlowFormBackBid> getExtBackBidByFormId(String formId) {
        Map<String,Object> params= Maps.newHashMap();
        params.put("formId",formId);
        return extWFlowFormBackBidMapper.getBackBidByMap(params);
    }

    @Override
    public boolean updateTechnicalScoreByFormIdAndSupplierId(String formId, String suplierId,Double score,boolean qualified) {
        WFlowFormBackBid wFlowFormBackBid = new WFlowFormBackBid();
        wFlowFormBackBid.setFlowFormId(formId);
        wFlowFormBackBid.setSupplierId(suplierId);
        wFlowFormBackBid.setDelFlag(CommonConstants.DEL_FLAG_NORMAL);
        EntityWrapper entityWrapper = new EntityWrapper();
        entityWrapper.setEntity(wFlowFormBackBid);

        WFlowFormBackBid wFlowFormBackBid1 = new WFlowFormBackBid();
        wFlowFormBackBid1.setTechnicalScore(score);
        if(qualified){
            wFlowFormBackBid1.setQualificationOrNot(CommonConstants.BID_OK);
        }else{
            wFlowFormBackBid1.setQualificationOrNot(CommonConstants.BID_NO);
            wFlowFormBackBid1.setAbandonedBidOrNot(CommonConstants.YES_INTEGER);
            wFlowFormBackBid1.setAbandonedBidTime(DateUtils.getNow());
            wFlowFormBackBid1.setInvalidFlag(CommonConstants.YES_INTEGER);
        }
        return update(wFlowFormBackBid1,entityWrapper);
    }

    @Override
    public boolean updateBackBidStatusByFormId(String formId, Integer status) {
        WFlowFormBackBid wFlowFormBackBid = new WFlowFormBackBid();
        wFlowFormBackBid.setFlowFormId(formId);
        EntityWrapper entityWrapper = new EntityWrapper();
        entityWrapper.setEntity(wFlowFormBackBid);
        WFlowFormBackBid wFlowFormBackBid1 = new WFlowFormBackBid();
        wFlowFormBackBid1.setBackBidOrNot(status);
        return update(wFlowFormBackBid1,entityWrapper);
    }

    @Override
    public boolean updateBySuppllierIdAndFormIdAndSn(WFlowFormBackBid wFlowFormBackBid) {
        WFlowFormBackBid where = new WFlowFormBackBid();
        where.setFlowFormId(wFlowFormBackBid.getFlowFormId());
        where.setSupplierId(wFlowFormBackBid.getSupplierId());
        where.setSegmentsNumber(wFlowFormBackBid.getSegmentsNumber());
        EntityWrapper entityWrapper = new EntityWrapper();
        entityWrapper.setEntity(where);
        return update(wFlowFormBackBid,entityWrapper);
    }

    @Override
    public boolean setInvild(WFlowFormBackBid where) {
        EntityWrapper entityWrapper = new EntityWrapper();
        entityWrapper.setEntity(where);
        WFlowFormBackBid wFlowFormBackBid = new WFlowFormBackBid();
        wFlowFormBackBid.setInvalidFlag(CommonConstants.YES_INTEGER);
        return update(wFlowFormBackBid,entityWrapper);
    }

    @Override
    public boolean grenerateDarkCodeByFormIdAndSupplierId(String darkCode, String formId, String supplierId) {
        WFlowFormBackBid where = new WFlowFormBackBid();
        where.setFlowFormId(formId);
        where.setSupplierId(supplierId);
        EntityWrapper entityWrapper = new EntityWrapper();
        entityWrapper.setEntity(where);
        WFlowFormBackBid entity = new WFlowFormBackBid();
        entity.setDarkBidCode(darkCode);
        return update(entity,entityWrapper);
    }

    @Override
    public List<String> selectSupplieridsWithBackBid(String formId) {
        return extWFlowFormBackBidMapper.selectSupplieridsWithBackBid(formId);
    }

    @Override
    public List<String> selectSupplieridsByMap(Map<String, Object> param) {
        return extWFlowFormBackBidMapper.selectSupplieridsByMap(param);
    }

    @Override
    public List<String> selectSupplierSysIdsByMap(Map<String, Object> map) {
        return extWFlowFormBackBidMapper.selectSupplierSysIdsByMap(map);
    }

    @Override
    public WFlowFormBackBid selectOneByFormIdAndSupplierIdAndSn(String fromId, String supplierid, Integer sn) {
        WFlowFormBackBid where = new WFlowFormBackBid();
        where.setFlowFormId(fromId);
        where.setSupplierId(supplierid);
        where.setSegmentsNumber(sn);
        EntityWrapper entityWrapper = new EntityWrapper();
        entityWrapper.setEntity(where);
        return selectOne(entityWrapper);
    }

    @Override
    public List<WFlowFormBackBid> getByIds(List<String> ids) {
        EntityWrapper entityWrapper = new EntityWrapper();
        entityWrapper.in("id",ids);
        return selectList(entityWrapper);
    }

    @Override
    public List<IdName> selectIdAndNameByMap(Map<String, Object> map) {
        return extWFlowFormBackBidMapper.selectIdAndNameByMap(map);
    }


    @Override
    public void updateAwardBidByFormId(String formId) {
        WFlowFormBackBid where = new WFlowFormBackBid();
        where.setFlowFormId(formId);
        EntityWrapper entityWrapper = new EntityWrapper();
        entityWrapper.setEntity(where);

        WFlowFormBackBid wFlowFormBackBid = new WFlowFormBackBid();
        wFlowFormBackBid.setAwardBidOrNot(CommonConstants.AWARD_BID);
        update(wFlowFormBackBid,entityWrapper);
    }

    @Override
    public Map<String, String> querySupplierIdAndDrakCode(String fromId) {
        return extWFlowFormBackBidMapper.querySupplierIdAndDrakCodeByFormId(fromId);
    }
}
